<template>
    <el-card class="register" v-if="handleType == 'register'">
        <div class="header">人力资源管理系统注册</div>
        <el-form :model="form" @submit.prevent="handleRegister">

            <el-form-item>
                <el-icon size="25">
                    <User />
                </el-icon>
                <el-input v-model="form.username" placeholder="请输入用户名" required></el-input>
            </el-form-item>

            <el-form-item>
                <el-icon size="25">
                    <Lock />
                </el-icon>
                <el-input show-password type="password" v-model="form.password" placeholder="请输入密码" required></el-input>
            </el-form-item>
            <el-form-item>
                <el-icon size="25">
                    <Message />
                </el-icon>
                <el-input v-model="form.email" placeholder="请输入邮箱地址" required></el-input>
            </el-form-item>
        </el-form>
        <el-button type="primary" @click="handleRegister">快速注册</el-button>
        <div class="register">已有账号，<span style="color: blue;cursor: pointer;"
                @click="handleChangeType('login')">直接登录</span>
        </div>

    </el-card>
</template>
<script setup lang="ts">
import { ref, defineProps, defineEmits } from "vue"
import { createData } from "../../../api/api"

const props = defineProps({
    handleType: String
})
const emit = defineEmits()

const handleChangeType = (type: string) => {
    //在跳转前清空表单
    resetForm()
    emit("handleChangeType", type)
}

let form = ref({
    employee_id: null,
    username: '',
    password: '',
    email: '',
    role: 'employee'
});

let employeeForm = ref({
    "employee_id": null,
    "name": null,
    "gender": null,
    "date_of_birth": null,
    "email": null,
    "phone_number": null,
    "hire_date": null,
    "position": null,
    "department": null,
    "salary": null,
    "address": null,
    "status": "在岗"
}
)

const handleRegister = async () => {
    //同步邮箱
    employeeForm.value.email = form.value.email

    const res = await createData("/employees", employeeForm.value)
    if (res.employee_id) {
        form.value.employee_id = res.employee_id
        const res2 = await createData("/accounts", form.value)
        if (res2.code != 200) return
        //注册成功后跳转登录页面
        handleChangeType('login')
        //填充其余信息
        employeeForm.value.employee_id = res.employee_id
        emit("openDialog", employeeForm.value)

    }
}
const resetForm = () => {
    form.value = {
        employee_id: null,
        username: '',
        password: '',
        email: '',
        role: 'employee'
    }
}
</script>
<style scoped lang="scss">
.el-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    padding: 20px 0;

    :deep() .el-card__body {
        padding: 0 !important;
        width: 80%;
    }

    .header {
        width: 100%;
        text-align: center;
        font-size: 20px;
        padding: 20px 0;
        letter-spacing: 3px;
    }

    .el-form {
        margin-top: 20px;

        .el-form-item {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;


            .el-input {
                width: 85%;
                margin-left: 10px;
            }

            &:not(:last-child) {
                margin-bottom: 20px;
            }
        }
    }

    .password {
        width: 100%;
        margin-bottom: 20px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: -10px;

        .el-checkbox {
            width: 100px;
        }
    }

    .el-button {
        width: 100%;
        height: 40px;
        letter-spacing: 3px;
        font-size: 18px;
    }

    .register {
        width: 100%;
        margin-top: 20px;
        text-align: center;
    }
}
</style>